<?php

namespace app\admin\controller;

use app\common\controller\Backend;
use app\common\model\User;
use think\Config;

/**
 * 控制台
 *
 * @icon fa fa-dashboard
 * @remark 用于展示当前系统中的统计数据、统计报表及重要实时数据
 */


/***
 * ┌───┐   ┌───┬───┬───┬───┐ ┌───┬───┬───┬───┐ ┌───┬───┬───┬───┐ ┌───┬───┬───┐
 * │Esc│   │ F1│ F2│ F3│ F4│ │ F5│ F6│ F7│ F8│ │ F9│F10│F11│F12│ │P/S│S L│P/B│  ┌┐    ┌┐    ┌┐
 * └───┘   └───┴───┴───┴───┘ └───┴───┴───┴───┘ └───┴───┴───┴───┘ └───┴───┴───┘  └┘    └┘    └┘
 * ┌───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───────┐ ┌───┬───┬───┐ ┌───┬───┬───┬───┐
 * │~ `│! 1│@ 2│# 3│$ 4│% 5│^ 6│& 7│* 8│( 9│) 0│_ -│+ =│ BacSp │ │Ins│Hom│PUp│ │N L│ / │ * │ - │
 * ├───┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─────┤ ├───┼───┼───┤ ├───┼───┼───┼───┤
 * │ Tab │ Q │ W │ E │ R │ T │ Y │ U │ I │ O │ P │{ [│} ]│ | \ │ │Del│End│PDn│ │ 7 │ 8 │ 9 │   │
 * ├─────┴┬──┴┬──┴┬──┴┬──┴┬──┴┬──┴┬──┴┬──┴┬──┴┬──┴┬──┴┬──┴─────┤ └───┴───┴───┘ ├───┼───┼───┤ + │
 * │ Caps │ A │ S │ D │ F │ G │ H │ J │ K │ L │: ;│" '│ Enter  │               │ 4 │ 5 │ 6 │   │
 * ├──────┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴────────┤     ┌───┐     ├───┼───┼───┼───┤
 * │ Shift  │ Z │ X │ C │ V │ B │ N │ M │< ,│> .│? /│  Shift   │     │ ↑ │     │ 1 │ 2 │ 3 │   │
 * ├─────┬──┴─┬─┴──┬┴───┴───┴───┴───┴───┴──┬┴───┼───┴┬────┬────┤ ┌───┼───┼───┐ ├───┴───┼───┤ E││
 * │ Ctrl│    │Alt │         Space         │ Alt│    │    │Ctrl│ │ ← │ ↓ │ → │ │   0   │ . │←─┘│
 * └─────┴────┴────┴───────────────────────┴────┴────┴────┴────┘ └───┴───┴───┘ └───────┴───┴───┘
 *
 */
class Dashboard extends Backend
{

    /**
     * 查看
     */
    public function index()
    {
        $seventtime = \fast\Date::unixtime('day', -15);
        //from_unixtime
        $group = 'FROM_UNIXTIME(createtime,"%Y-%m-%d")';
        //订单总数
        $createdata = \app\common\model\Order::where('createtime','>=',$seventtime)
            ->field('count(*) as orderNum,FROM_UNIXTIME(createtime,"%Y-%m-%d") as date')
            ->group($group)
            ->select();
        //成交数
        $paydata = \app\common\model\Order::where('createtime','>=',$seventtime)
            ->field('count(*) as transactionsNum,FROM_UNIXTIME(createtime,"%Y-%m-%d") as date')
            ->where('status','>',1)
            ->where('status','<',6)
            ->group($group)
            ->select();
//        dd($paydata);
        $createlistnew = $paylistnew   = $paydate = $day = $paylist = $createlist = [];

        for ($i=1;$i<7;$i++){
            $day[]=date('Y-m-d',$seventtime+($i*86400));
        }
        //获取有成交的日期
        for ($i=0;$i<count($paydata);$i++){
            $paydate[]=$paydata[$i]['date'];
        }
//        dd($paydate);
        //获取有订单日期
        for ($i=0;$i<count($createdata);$i++){
            $createdatas[]=$createdata[$i]['date'];
        }
        //计算差值
        $paylists = array_diff($day,$paydate);

        $createlistdata = array_diff($day,$createdatas);
        //默认数组下表0
        foreach ($paylists as $k1=>$v1){
            $paylistnew[]=[
                'transactionsNum'=>0,
                'date'=>$v1
            ];
        }

        foreach ($createlistdata as $k1=>$v1){
            $createlistnew[]=[
                'orderNum'=>0,
                'date'=>$v1
            ];
        }
        //合并有效数据
        $newarray = array_merge($paydata,$paylistnew);
        $newcreatearray = array_merge($createdata,$createlistnew);


        //根据时间排序 array_column(数组,数组中的某个键值)
        $arr = array_column($newarray,'date');
        array_multisort($arr,SORT_ASC,$newarray);
        $arr = array_column($newcreatearray,'date');
        array_multisort($arr,SORT_ASC,$newcreatearray);

        //数组去重
        $paylistunique = assoc_unique($newarray,'date');
        $createlistunique= assoc_unique($newcreatearray,'date');

//        dump($newarray);
//        dump($paylistunique);

        foreach ($paylistunique as $k=>$v){
            $paylist[$v['date']] = $v['transactionsNum'];
        }
//        dd($paylist);
        foreach ($createlistunique as $k=>$v){
            $createlist[] = $v['orderNum'];
        }
//dump($paylist);
//dd($createlist);

//        $hooks = config('addons.hooks');
//        $uploadmode = isset($hooks['upload_config_init']) && $hooks['upload_config_init'] ? implode(',', $hooks['upload_config_init']) : 'local';
//        $addonComposerCfg = ROOT_PATH . '/vendor/karsonzhang/fastadmin-addons/composer.json';
//        Config::parse($addonComposerCfg, "json", "composer");
//        $config = Config::get("composer");
//        $addonVersion = isset($config['version']) ? $config['version'] : __('Unknown');
        $totaluser = User::count();//总会员
        $totalorder = \app\common\model\Order::where('parent_id','<','0')->count();//总订单
        $totalorderamount = \app\common\model\Order::where('parent_id','<','0')
            ->where('status','>',1)
            ->where('status','<',5)
            ->sum('pay_price');//总金额
        $xian_goods = \app\common\model\GoodsDonate::whereNull('deletetime')
            ->count();//闲置商品
        $bao_goods = \app\common\model\Goods::whereNull('deletetime')
            ->count();//爆品
        $dui_goods = \app\common\model\Goods1::whereNull('deletetime')
            ->count();//兑换商品
        $dui_s_goods = \app\common\model\Goods1::whereNull('deletetime')
            ->where('state',1)
            ->count();//兑换商品-上架数量
        $xian_s_goods = \app\common\model\GoodsDonate::whereNull('deletetime')
            ->where('state',1)
            ->count();//闲置商品-上架数量
        $bao_s_goods = \app\common\model\Goods::whereNull('deletetime')
            ->where('state',1)
            ->count();//闲置商品-上架数量
        $this->view->assign([
            'totaluser'        => $totaluser,
//            'totalviews'       => 219390,
            'totalorder'       => $totalorder,
            'totalorderamount' => $totalorderamount,
            'xian_goods' => $xian_goods,
            'bao_goods' => $bao_goods,
            'dui_goods' => $dui_goods,
            'dui_s_goods' => $dui_s_goods,
            'xian_s_goods' => $xian_s_goods,
            'bao_s_goods' => $bao_s_goods,
//
//            'sevendnu'         => '80%',
//            'sevendau'         => '32%',
            'paylist'          => $paylist,
            'createlist'       => $createlist,
//            'addonversion'       => $addonVersion,
//            'uploadmode'       => $uploadmode
        ]);

        return $this->view->fetch();
    }

}
